<template>
  <div class="goodsCate">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-button type="primary" @click="showAddCate">添加分类</el-button>
      <!-- tree -->
      <zk-table
        :data="cateList"
        :columns="columns"
        :selection-type="false"
        show-index
        index-text="ID"
        :expand-type="false"
        border
        :show-row-hover="false"
      >
        <template slot="isOk" slot-scope="scope">
          <i style="color:lightgreen" class="el-icon-success" v-if="scope.row.cat_deleted===false"></i>
          <i style="color:red" class="el-icon-error" v-else></i>
        </template>
        <template slot="level" slot-scope="scope">
          <el-tag size="mini" type="success" v-if="scope.row.cat_level===0">等级一</el-tag>
          <el-tag size="mini" type="info" v-else-if="scope.row.cat_level===1">等级二</el-tag>
          <el-tag size="mini" type="warning" v-else>等级三</el-tag>
        </template>
        <template slot="control" slot-scope="scope">
          <el-button type="primary" size="mini" @click="handelEdit(scope.row)">编辑</el-button>
          <el-button type="warning" size="mini" @click="handelDelete(scope.row)">删除</el-button>
        </template>
      </zk-table>
      <!-- pagination -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="requerList.pagenum"
        :page-sizes="[5, 8, 10, 15]"
        :page-size="requerList.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <!-- 添加Dialog -->
    <el-dialog title="添加分类" :visible.sync="addCate" width="50%" @close="addCateDialog">
      <el-form :model="addCateForm" :rules="rules" ref="addCateRef" label-width="100px">
        <el-form-item label="名称分类" prop="cat_name">
          <el-input v-model="addCateForm.cat_name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="选择分类">
          <el-cascader
            :props="props"
            v-model="parentCateKey"
            :options="cataParentLists"
            expandTrigger="hover"
            @change="handleChange"
            change-on-select
            clearable
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCate = false">取 消</el-button>
        <el-button type="primary" @click="addCateForms()">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 编辑Dialog -->
    <el-dialog title="编辑分类" :visible.sync="editCateDialog" width="50%" @close="editCateDialogClose">
      <el-form :model="addCateForm" ref="editCateRef" label-width="100px">
        <el-form-item label="名称分类" prop="cat_name">
          <el-input v-model="editCateForm.cat_name" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editCateDialog = false">取 消</el-button>
        <el-button type="primary" @click="editCateName">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: 'goodsCate',
  data() {
    return {
      cateList: [],
      requerList: {
        pagenum: 1,
        pagesize: 5,
        type: 3
      },
      addCate: false,
      total: 0,
      editCateDialog: false,
      addCateForm: {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0
      },
      editCateForm: { cat_id: '', cat_name: '' },

      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          prop: 'cat_deleted',
          type: 'template',
          template: 'isOk'
        },
        {
          label: '等级',
          prop: 'cat_level',
          type: 'template',
          template: 'level'
        },
        {
          label: '操作',
          type: 'template',
          template: 'control'
        }
      ],
      cataParentLists: [],
      rules: {
        cat_name: [
          { required: true, message: '分类名称不能为空', trigger: 'blur' }
        ]
      },
      editRules: {
        cat_name: [
          { required: true, message: '分类名称不能为空', trigger: 'blur' }
        ]
      },
      props: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      },
      parentCateKey: []
    }
  },
  created() {
    this.getCateList()
  },
  methods: {
    getCateList() {
      this.$axios
        .get('categories', { params: this.requerList })
        .then(result => {
          //   console.log(result)
          this.cateList = result.data.data.result
          this.total = result.data.data.total
          // console.log(this.cateList)
        })
    },
    handleSizeChange(val) {
      //   console.log('每页显示改变了', val)
      this.requerList.pagesize = val
      this.getCateList()
    },
    handleCurrentChange(val) {
      this.requerList.pagenum = val
      this.getCateList()
    },
    handelEdit(row) {
      // console.log(row)
      this.editCateDialog = true
      this.editCateForm.cat_name = row.cat_name
      this.editCateForm.cat_id = row.cat_id
      // console.log(this.editCateForm)
    },
    handelDelete(row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$axios.delete(`categories/${row.cat_id}`).then(result => {
            console.log(result)
            this.$message({
              type: 'success',
              message: result.data.meta.msg
            })
            this.getCateList()
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    showAddCate() {
      this.getCateParent()
      this.addCate = true
    },
    getCateParent() {
      this.$axios.get('categories', { params: { type: 2 } }).then(result => {
        this.cataParentLists = result.data.data
        // console.log(this.cataParentLists, 1)
      })
    },
    handleChange() {
      if (this.parentCateKey.length > 0) {
        this.addCateForm.cat_pid = this.parentCateKey[
          this.parentCateKey.length - 1
        ]
        this.addCateForm.cat_level = this.parentCateKey.length
      } else {
        this.addCateForm.cat_pid = 0
        this.addCateForm.cat_level = 0
      }
      // console.log(this.addCateForm)
    },
    addCateForms() {
      // console.log(this.addCateForm)
      this.$refs.addCateRef.validate(valid => {
        if (valid) {
          this.$axios.post('categories', this.addCateForm).then(result => {
            this.$message({
              type: 'success',
              message: result.data.meta.msg
            })
            this.getCateList()
            this.addCate = false
          })
        } else {
          this.$message({
            type: 'error',
            message: '请输入分类名称'
          })
          return false
        }
      })
    },
    addCateDialog() {
      this.$refs.addCateRef.resetFields()
      this.parentCateKey = []
      this.addCateForm.cat_pid = 0
      this.addCateForm.cat_level = 0
      // console.log(this.parentCateKey)
      // console.log(this.addCateForm)
    },
    editCateDialogClose() {
      this.$refs.editCateRef.resetFields()
    },
    editCateName() {
      this.$refs.editCateRef.validate(valid => {
        if (valid) {
          // console.log(this.editCateForm)
          this.$axios
            .put(`categories/${this.editCateForm.cat_id}`, {
              cat_name: this.editCateForm.cat_name
            })
            .then(result => {
              console.log(result)
              this.$message({
                type: 'success',
                message: result.data.meta.msg
              })
              this.getCateList()
              this.editCateDialog = false
            })
        } else {
          this.$message({
            type: 'error',
            message: '请输入分类名称'
          })
          return false
        }
      })
    }
  }
}
</script>
<style scoped>
</style>